package com.glory.brepda.activity.lanjian;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.LastBatchAdapter;
//import com.glory.brepda.adapter.WaferLotHpAdapter;
import com.glory.brepda.base.BaseActivity;
//import com.glory.brepda.entity.WaferHP;
import com.glory.brepda.entity.WoPartWobomline;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BreWGJActivity extends BaseActivity {

    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
//    @BindView(R.id.etEqpId)
//    FreeUI_ClearEditText etEqpId;
//    @BindView(R.id.etCust)
//    FreeUI_ClearEditText etCust;
//    @BindView(R.id.layoutCust)
//    LinearLayout layoutCust;
//    @BindView(R.id.etMMT)
//    FreeUI_ClearEditText etMMT;
//    @BindView(R.id.layoutMmt)
//    LinearLayout layoutMmt;
    @BindView(R.id.etproductModuct)
    FreeUI_ClearEditText etproductModuct;
//    @BindView(R.id.layoutProduct)
//    LinearLayout layoutProduct;
    @BindView(R.id.etWaferId)
    FreeUI_ClearEditText etWaferId;
    @BindView(R.id.ethyh)
    FreeUI_ClearEditText ethyh;
//    @BindView(R.id.etWaferLot)
//    FreeUI_ClearEditText etWaferLot;
    @BindView(R.id.etWoId)
    FreeUI_ClearEditText etWoId;
    @BindView(R.id.ethp)
    FreeUI_ClearEditText ethp;
    @BindView(R.id.etch)
    FreeUI_ClearEditText etch;
    @BindView(R.id.etms)
    FreeUI_ClearEditText etms;
    @BindView(R.id.etlg)
    FreeUI_ClearEditText etlg;
    @BindView(R.id.etqt)
    FreeUI_ClearEditText etqt;
    @BindView(R.id.etjl)
    FreeUI_ClearEditText etjl;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.etbeizu)
    FreeUI_ClearEditText etbeizu;
//    @BindView(R.id.tvADllOne)
//    TextView tvADllOne;
//    @BindView(R.id.tvDllAll)
//    TextView tvDllAll;
//    @BindView(R.id.tvADd)
//    TextView tvADd;

//    @BindView(R.id.rvLastBatchList)
//    RecyclerView rvLastBatchList;

//    private WaferLotHpAdapter lastBatchAdapter;
    private WoPartWobomline woPartWobomline;

//    WaferHP hp = new WaferHP();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bre_wgjactivity;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

//        layoutCust.setVisibility(View.GONE);
//        layoutMmt.setVisibility(View.GONE);
//        layoutProduct.setVisibility(View.GONE);

//        initRecyclerView(rvLastBatchList,this);
//        lastBatchAdapter = new WaferLotHpAdapter(null);
//        lastBatchAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//        lastBatchAdapter.isFirstOnly(false);
//        rvLastBatchList.setAdapter(lastBatchAdapter);

        initEdit(etWaferId, sa -> {
            //通过片号获取相关信息
            FreeUI_EntityCallBack<BaseResponseBean<WoPartWobomline>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WoPartWobomline>>
                    (new TypeToken<BaseResponseBean<WoPartWobomline>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<WoPartWobomline>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        woPartWobomline = response.body().getResponse().getBody();
                        initInfoView(woPartWobomline);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<WoPartWobomline>> response) {
                    super.onError(response);
                    loadError(response.getException(), "失败");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("etWaferId", getText(etWaferId));
            map.addProperty("ACTION", "WAFER_QUERY");
            NetUtils.requestPostNet("IF.WORDER_QUERY_INFOS", "Common", getApplication(), map, callBack);
        });

    }

    private void initInfoView(WoPartWobomline woPartWobomline){
//        etCust.setText(woPartWobomline.getWORKORDER().getCUSTOMERCODE());
//        etMMT.setText(woPartWobomline.getWORKORDER().getUDF().getMainMatType());
//        etproductModuct.setText(woPartWobomline.getPART().getSPEC2());
        // 芯片型号，存在多芯情况
//        String wafers = "";
//        for(WoPartWobomline.XWorkOrderBomLine item :woPartWobomline.getWORKORDERBOMLINELIST()){
//            if(item.getMaterialName().startsWith("15001")){
//                wafers+=item.getMATERIALDESC()+",";
//            }
//        }
//        etWaferId.setText(wafers.substring(0,wafers.length()-1));
        etWoId.setText(woPartWobomline.getWoId());
        ethyh.setText(woPartWobomline.getHyh());
        etproductModuct.setText(woPartWobomline.getPdm());
        etEqp.setText(woPartWobomline.getEqpId());
    }

    @Override
    protected void initData() {

    }

    @OnClick({ R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
//                submit(lastBatchAdapter.getData());
                submit();
                break;
//            case R.id.tvADd:
//                addInit();
//                break;
//            case R.id.tvADllOne:
//                int n = lastBatchAdapter.getData().size();
//                if(n > 0 ){
//                    lastBatchAdapter.remove(n-1);
//                    lastBatchAdapter.notifyDataSetChanged();
//                }
//                break;
//            case R.id.tvDllAll:
//                lastBatchAdapter.getData().clear();
//                lastBatchAdapter.notifyDataSetChanged();
//                break;

        }
    }

    private void submit(){

//        if(dates.size() == 0 || StringUtils.isEmpty(getText(etEqpId)) || StringUtils.isEmpty(getText(etEqp))){
//            showToast("必填项不允许为空！",false);
//            return;
//        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("成功", true);
                    clearAll();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "WORDER_QUERY_INFOS");
            }
        };
        JsonObject map = new JsonObject();
//        map.addProperty("CUSTOMERCODE", getText(etCust));
//        map.addProperty("MAINMATTYPE", getText(etMMT));
        map.addProperty("WORDER", getText(etWoId));
        map.addProperty("ethyh", getText(ethyh));
        map.addProperty("etWaferId", getText(etWaferId));
        map.addProperty("SPEC2", getText(etproductModuct));
        map.addProperty("etEqp", getText(etEqp));

        map.addProperty("etHp", getText(ethp));
        map.addProperty("etCh", getText(etch));
        map.addProperty("etMs", getText(etms));
        map.addProperty("etLg", getText(etlg));
        map.addProperty("etQt", getText(etqt));

        map.addProperty("etJl", getText(etjl));
        map.addProperty("etBZ", getText(etbeizu));

//        map.add("WaferList", new JsonParser().parse(new Gson().toJson(lastBatchAdapter.getData())).getAsJsonArray());
        map.addProperty("ACTION","SAVE_WAFER_WGJ");
        NetUtils.requestPostNet("IF.WORDER_QUERY_INFOS", "Common", BreWGJActivity.this, map, callBack);
    }

    private void clearAll(){

//        etCust.setText(null);
//        etMMT.setText(null);
//        etproductModuct.setText(null);
//        etWaferId.setText(null);
//        ethyh.setText(null);
//        etEqp.setText(null);
//        etWaferLot.setText(null);
//        ethp.setText(null);
//        etch.setText(null);
//        etms.setText(null);
//        etlg.setText(null);
//        etqt.setText(null);
//        etjl.setText(null);
//        lastBatchAdapter.getData().clear();
//        etWoId.setText(null);
//        ethyh.setText(null);
//        etproductModuct.setText(null);
//        etEqp.setText(null);

        etWoId.setText(null);
        ethyh.setText(null);
        etWaferId.setText(null);
        etproductModuct.setText(null);
        etEqp.setText(null);

        ethp.setText(null);
        etch.setText(null);
        etms.setText(null);
        etlg.setText(null);
        etqt.setText(null);

        etjl.setText(null);
        etbeizu.setText(null);

    }

//    private void addInit(){

        // 片号重复情况
//        List<WaferHP> dates =  lastBatchAdapter.getData();
//        for(WaferHP item : dates){
//            if(getText(etWaferLot).contains(item.getEtWaferLot())){
//                showToast("当前芯片Lot号在列表中已存在，请勿重复添加！",false);
//                return;
//            }
//        }
//
//        hp.setEtEqp(getText(etEqp));
//        hp.setEtWaferLot(getText(etWaferLot));
//        hp.setEthp(getText(ethp));
//        hp.setEtch(getText(etch));
//        hp.setEtms(getText(etms));
//        hp.setEtlg(getText(etlg));
//        hp.setEtqt(getText(etqt));
//        hp.setEtJl(getText(etjl));
//
//        lastBatchAdapter.addData(hp);
//        etWaferLot.setText(null);
//        etWaferLot.requestFocus();
//
//        ethp.setText("0");
//        etch.setText("0");
//        etms.setText("0");
//        etlg.setText("0");
//        etqt.setText(null);
//        hp = new WaferHP();
//    }
}